package com.lele.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.lele.org.service.ConsumerService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class ConsumerServiceImpl implements ConsumerService {

	@Autowired
	private RestTemplate restTemplate;
	
	@HystrixCommand(fallbackMethod ="errorMsg")//出现错误时的回调方法
	//@HystrixCommand(fallbackMethod = "myFallback")
	@Override
	public String consumer() {
		//调用hello-service服务，注意这里用的是服务名，而不是具体的ip+port
		String result = restTemplate.getForObject("http://hello-service/refactor/hello", String.class);
		System.out.println("调用成功：" + result);
		return "调用成功：" + result;
	}

	@Override
	public String errorMsg() {
		return "error!!!";
	}

	   /**
     * 方法simpleHystrixClientCall的回退方法，可以指定将hystrix执行失败异常传入到方法中
     * @param p ystrix执行失败的传入方法的请求
     * @param e hystrix执行失败的异常对象
     * @return
     */
//    private String myFallback(Throwable e) {
//    	e.printStackTrace();
//        return "Execute raw fallback: access service fail,reason = " + e;
//    }
}
